define(function(require, exports, module) {
    'use strict';

    var rk = require('rk');
    require('crm/js/core/grid/grid');

    return {
        init: function() {
            var me = this;
            var elem = $('#rk-panel');
            //获取联系方式
            var contactWay = "";
            rk.post("/feedback/getUserContactWay.action", {}, function(json) {
                if (json.status == 0) {
                    contactWay = json.data.contactWay;
                    var html = require('page/tmpl/feedback/index.tpl');

                    elem.html(rk.templateText(html, {
                        contactWay: contactWay
                    }));
                    rk.showPageView('rk-panel', "意见反馈");

                    //上传图片
                    var $upload = elem.find('.fileinput-box');
                    $upload.multiimageuploader({
                        url: '/json/global_uploadify/apply-upload.action',
                        queueSelector: 'ul[act="feedback_ul"]',
                        queueOptions: {
                            // loadingTemplate: '<li class="uploadqueue-item"><span class="operat_area"><span class="cover_layer"></span><a href="javascript:;" class="close_btn uploadqueue-item-close"></a></span></li>',
                            loadingTemplate: '<li class="uploadqueue-item"><img src="{{fileUrl}}"/><span class="operat_area"><span class="cover_layer"></span><a href="javascript:;" class="close_btn uploadqueue-item-close"></a></span></li>',

                            sizeKey: 'filelength',
                            resultParser: function(json) {
                                // {
                                //     file: ""
                                //     fileId: ""
                                //     fileName: "40.jpg"
                                //     fileUrl: "https://devrs.s3.cn-north-1.amazonaws.com.cn/apply/2016/03/01/86075c51-55fe-47a9-af70-da2815f467ce.jpg"
                                //     filelength: "99235"
                                //     status: 0
                                //     statusText: "??????"
                                // }
                                return json;
                            }
                        },
                        limitFileCount: 9,
                        itemAdd: function(e, data) {
                            elem.find('li[act="feedback_li_parant"]').show();
                        },
                        itemRemove: function(e, data) {
                            $(this).multiimageuploader('val').length || elem.find('li[act="feedback_li_parant"]').hide();
                        }
                    });

                    //提交事件
                    elem.on("click", "[act='feedbackCreate']", function() {
                        var content = elem.find('textarea[act="content"]');
                        var contactWay = elem.find('input[act="contactWay"]');
                        if ($.trim(content.val()) == null || $.trim(content.val()) == undefined || $.trim(content.val()) == "") {
                            $.msg("反馈内容必填！");
                            content.focus();
                            return false;
                        }
                        if ($upload.multiimageuploader('status').uploading) {
                            return $.msg(rk.i18n('文件上传中...'));
                        }
                        var fileArray = $upload.multiimageuploader('val').map(function(file) {
                            return {
                                fileName: file.fileName,
                                fileUrl: file.fileUrl,
                                fileLength: file.filelength
                            }
                        });
                        //var blockWin = '<script id="scrpt_tmpl_container_feedback_block"><div>提交中...</div></script>';
                        $.openPopupLayer({
                            name: "feedbackBlock",
                            title: "xxx",
                            width: 620,
                            target: "scrpt_tmpl_container_feedback_block"
                        });

                        var createBtn = elem.find('a[act="feedbackCreate"]');
                        createBtn.prop('disabled', true);
                        createBtn.html("提交中...");
                        createBtn.removeClass("pg-btn-blue");
                        createBtn.addClass("pg-btn-disabled");

                        $.postJson('/feedback/add.action', {
                            content: content.val(),
                            contactWay: contactWay.val(),
                            feedbackImgs: JSON.stringify(fileArray),
                            version: SESSION.currentVersion
                        }).done(function(json) {
                            if (json.status == 0) {
                                //$.msg(rk.i18n('SUCCESS'), 1);
                                rk.noticeSuccess(rk.i18n('SUCCESS'));
                                content.val("");
                                //contactWay.val("");
                                var feedback_ul = elem.find('ul[act="feedback_ul"]');
                                feedback_ul.html("");
                                fileArray = [];
                            }
                            createBtn.prop('disabled', false);
                            createBtn.html("提交");
                            createBtn.removeClass("pg-btn-disabled");
                            createBtn.addClass("pg-btn-blue");

                            elem.find('li[act="feedback_li_parant"]').hide();
                            $.closePopupLayer();
                        }).fail(function(def, code, message) {
                            //def.status != 0 && $.msg(rk.i18n('REQUEST_ERROR'));
                            rk.noticeError(rk.i18n('REQUEST_ERROR'));
                            createBtn.prop('disabled', false);
                            createBtn.html("提交");
                            createBtn.removeClass("pg-btn-disabled");
                            createBtn.addClass("pg-btn-blue");
                            $.closePopupLayer();
                        });
                    });

                    //tab页点击事件
                    elem.on("click", "[act='feedback-tab-add']", function() {
                        elem.find('div[act="feedback_add"]').show();
                        elem.find('li[act="feedback-tab-add"]').addClass("active");
                        elem.find('div[act="feedback_list"]').hide();
                        elem.find('li[act="feedback-tab-list"]').removeClass("active");
                    });
                    elem.on("click", "[act='feedback-tab-list']", function() {
                        elem.find('div[act="feedback_add"]').hide();
                        elem.find('li[act="feedback-tab-add"]').removeClass("active");
                        elem.find('div[act="feedback_list"]').show();
                        elem.find('li[act="feedback-tab-list"]').addClass("active");
                        $(window).on('resize', function() {
                            elem.find('div.feedback-tab-content').height(elem.find('div.feedback-tab').height() - 56);
                        }).trigger('resize');
                        //输出列表内容：反馈内容、处理状态、创建日期
                        //'id', 'content', 'dealStatus',  'createdAt'
                        var dataFields = ['id', 'createdAt', 'content', 'dealStatus'];
                        var cellsRender = {};
                        var gridOpt = {
                            itemsUrl: '/feedback/listNew.action',
                            searchUrl: '/feedback/search.action',
                            cellsRender: cellsRender,
                            dataFields: dataFields,
                            specialColumn: false,
                            dataRoot: 'data>feedbacks',
                            dataKeywords: 'feedbacks',
                            appName: 'feedback',
                            belongId: 9,
                            noSetColumn: true,
                            columnResizeDisabled: true,
                            selectRowDisabled: true,
                            pinnedRowCount: 0,
                            hideNullColumn: true,
                            dataBeforeProcessing: function(i, o, data, listdata, oValues, oTypes) {
                                $.each(o, function(k, v) {
                                    if (oTypes[k] == 3) {
                                        o[k + '_value'] = v && oValues[k][v] ? oValues[k][v].name : '';
                                    } else {
                                        o[k + '_value'] = v;
                                    }
                                });
                            }
                        };
                        $('#grid_con').find('#div_main_content').customerList(gridOpt);
                    });
                }
            }).fail(function(def, code, file) {
                def.status != 0 && $.msg(rk.i18n('REQUEST_ERROR'));
            });
        }
    }

});
